package com.tensquare.usr.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.usr.dao.AdminDao;
import com.tensquare.usr.pojo.Admin;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import sun.plugin.dom.core.Element;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder coder;
    @Autowired
    private HttpServletRequest request;


    /**
     * 查询全部列表
     * @return
     */
    public List<Admin> findAll() {
        return adminDao.selectList(null);
    }


    /**
     * 条件查询+分页
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Admin> findSearch(Map<String,Object> whereMap, int page, int size) {
        EntityWrapper<Admin> entityWrapper=new EntityWrapper();
        Set<String> set = whereMap.keySet();
        for (String s : set) {
            entityWrapper.eq(whereMap.get(s)!=null,s,whereMap.get(s));
        }
        Page<Admin>pageData=new Page<>(page,size);
        List<Admin> admins = adminDao.selectPage(pageData, entityWrapper);
        pageData.setRecords(admins);
        return  pageData;
    }


    /**
     * 条件查询
     * @param whereMap
     * @return
     */
    public List<Admin> findSearch(Map<String,Object> whereMap) {
        return adminDao.selectByMap(whereMap);
    }

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Admin findById(String id) {
        return adminDao.selectById(id);
    }

    /**
     * 增加
     * @param admin
     */
    public void add(Admin admin) {
        admin.setId( idWorker.nextId()+"" );
        //密码加密
        admin.setPassword(coder.encode(admin.getPassword()));
        adminDao.insert(admin);
    }

    /**
     * 修改
     * @param admin
     */
    public void update(Admin admin) {
        adminDao.updateById(admin);
    }

    public Admin login(Admin admin) {
        Admin adminlogin=adminDao.findByLoginName(admin.getLoginname());
        if(adminlogin!=null&&coder.matches(admin.getPassword(),adminlogin.getPassword())){
            return adminlogin;
        }
        return null;

    }


    /**
     * 删除 必须有admin角色才能删除
     * @param id
     */
    public void deleteById(String id) {

        Claims claims_user = (Claims) request.getAttribute("claims_admin");
        if(claims_user==null){
            throw new RuntimeException("权限不足");
        }
        adminDao.deleteById(id);
    }
}
